<template>
  <a-modal
    title="上传要素表"
    :width="460"
    :visible="visible"
    :confirmLoading="mloading"
    :maskClosable="false"
    @ok="onClickOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="mloading">
      <div class="import__box">
        <a-button
          type="primary"
          @click="onClickDown"
        >下载模版</a-button>
        <a-upload
          style="margin-left: 40px;"
          accept=".xlsx"
          :customRequest="customRequest1"
          :fileList="fileList1"
          :remove="handleRemove1"
        >
          <a-button>上传表格<a-icon v-if="model.excel_status" type="check-circle" style="color: #14A136;" /></a-button>
        </a-upload>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import {
  handleExport
} from '@/api/common'
import {
  cleanUpCases
} from '@/api/handle'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    model: {
      type: Object,
      default: () => {}
    }
  },

  data () {
    return {
      fileData: null,
      fileList1: [],
      mloading: false
    }
  },

  methods: {
    onClickDown () {
      this.handleDown()
    },

    handleDown () {
      const { uuid } = this.model || {}
      const params = new FormData()
      params.append('_uuid_', uuid)
      handleExport(params, '/file/handle/upload/case?download=tpl')
    },

    // 上传案件信息
    customRequest1 (options) {
      const { file } = options || {}
      const { name, uid } = file || {}
      this.fileData = file
      this.fileList1 = [{
        uid,
        name,
        status: 'done',
        url: ''
      }]
    },

    handleRemove1 () {
      this.fileList1 = []
    },

    handleImport () {
      if (!this.fileData) {
        this.$notification['error']({
          message: '提示',
          description: '请先上传表格后再提交哦',
          duration: 4
        })
      } else {
        this.mloading = true
        const { uuid } = this.model || {}
        const params = new FormData()
        params.append('_uuid_', uuid)
        params.append('file', this.fileData)
        return cleanUpCases(params).then((res) => {
          const { msg } = res
          this.$notification['success']({
            message: '提示',
            description: msg,
            duration: 4
          })
          // this.$emit('ok')
        }).catch((e) => {
          this.mloading = false
          this.$notification['error']({
            message: '提示',
            description: e.msg,
            duration: 4
          })
        })
      }
    },

    onClickOk () {
      this.handleImport()
    }
  }
}
</script>

<style lang="less" scoped>
.import__box {
  padding: 30px 90px;
}
</style>
